#import "OmMomentsVCCommentView.h"
#import "OmMomentsVCCommentViewSingleView.h"
@interface OmMomentsVCCommentView()
@property (nonatomic, strong) NSMutableArray<OmMomentsVCCommentViewSingleView *> *views;
@end
@implementation OmMomentsVCCommentView
- (void)setModelArray:(NSArray<OmCommentModel *> *)modelArray {
    _modelArray = modelArray;
    [self cleanViews];
    [self allocateViews];
    [self layoutViews];
}
- (void)cleanViews {
    for (OmMomentsVCCommentViewSingleView *view in self.views) {
        [view removeFromSuperview];
    }
    self.views = [NSMutableArray array];
}
- (void)allocateViews {
    for (OmCommentModel *model in self.modelArray) {
        OmMomentsVCCommentViewSingleView *commentSingleView = [[OmMomentsVCCommentViewSingleView alloc] init];
        commentSingleView.model = model;
        [self addSubview:commentSingleView];
        [self.views addObject:commentSingleView];
        if (model.child.count > 0) {
            for (OmCommentModel *childModel in model.child) {
                OmMomentsVCCommentViewSingleView *childCommentSingleView = [[OmMomentsVCCommentViewSingleView alloc] init];
                childCommentSingleView.model = childModel;
                [self addSubview:childCommentSingleView];
                [self.views addObject:childCommentSingleView];
            }
        }
    }
}
- (void)layoutViews {
    for (int i = 0; i < self.views.count; i++) {
        OmMomentsVCCommentViewSingleView *previousView = self.views[i-1];
        OmMomentsVCCommentViewSingleView *currentView = self.views[i];
        [currentView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.right.equalTo(self).offset(-10);
            if (i == 0) {
                make.top.equalTo(self).offset(10);
            } else {
                make.top.equalTo(previousView.mas_bottom).offset(20);
            }
            if (i == self.views.count - 1) {
                make.bottom.equalTo(self).offset(-10);
            }
        }];
        UIView *line = [[UIView alloc] init].fp_backgroundColor(HexadecimalColor(@"#E5E6E7"));
        [currentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(currentView.mas_bottom).offset(10);
            make.left.right.equalTo(self);
            make.height.equalTo(@(1));
        }];
    }
}
- (void)configSelf {
    self.backgroundColor = HexadecimalColor(@"#EDEDED");
    self.fp_cornerRadius(3).fp_clipsToBounds(YES);
}
@end
